package cn.qicaiwang.open.qiniu.config;

import com.qiniu.storage.Region;
import lombok.Data;
import org.yaml.snakeyaml.Yaml;


import java.io.InputStream;
import java.util.Map;
import java.util.Properties;

@Data


public class QiNiuConfig {
    private String accessKey;
    private String secretKey;
    private String bucket;
    private String domainOfBucket;
    private long expireInSeconds;
    private Region region;

    private QiNiuConfig() {
        try (InputStream inputStream = QiNiuConfig.class.getResourceAsStream("/application.yml")) {
            Yaml yaml = new Yaml();
            Map<String, Object> config = yaml.load(inputStream);
            Map<String, String> qiniuConfig = (Map<String, String>) config.get("qiniu");
            accessKey = qiniuConfig.get("access-key");
            secretKey = qiniuConfig.get("secret-key");
            bucket = qiniuConfig.get("bucket");
            domainOfBucket = qiniuConfig.get("domain-of-bucket");
            expireInSeconds = Long.parseLong(qiniuConfig.get("expire-in-seconds"));
            String zoneName = qiniuConfig.get("region");
            // 设置 region
            if (zoneName.equals("region0")) {
                region = Region.region0();
            } else if (zoneName.equals("region1")) {
                region = Region.region1();
            } else if (zoneName.equals("region2")) {
                region = Region.region2();
            } else if (zoneName.equals("regionAs0")) {
                region = Region.regionAs0();
            } else if (zoneName.equals("regionNa0")) {
                region = Region.regionNa0();
            } else {
                throw new Exception("Region对象配置错误！");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static QiNiuConfig instance = new QiNiuConfig();

    public static QiNiuConfig getInstance() {
        return instance;
    }
}